/*
 * @Author: 邵东亮 1762111402@qq.com
 * @Date: 2024-12-14 15:57:55
 * @LastEditors: 邵东亮 1762111402@qq.com
 * @LastEditTime: 2024-12-14 16:10:35
 * @Description:
 * @FilePath: \express\bigData_server\routes\login.js
 */
const express = require("express");
const router = express.Router();
const loginController = require("../controller/loginController");
/**
 *@api {get} api/captcha  获取验证码
 *@apiDescription  获取验证码功能说明
 *@apiName captcha
 *@apiGroup login
 *@apiSuccessExample {json} Success-Response:
 *{
  "data": "",
  "msg": "xDMu",
  "code": 200
 *}
 *@apiSampleRequest api/captcha
 *@apiVersion 1.0.0
 */
router.get("/captcha", loginController.captcha);

function login_middleware(req, res, next) {
  console.log("中间件1");
  next(); //传递给下一步
}

function login_params(req, res, next) {
  let { username, password } = req.body;
  console.log("中间件2");
  if (!username || !password) {
    //发送消息，结束响应，不需要再调用next
    res.json({
      message: "参数校验失败",
      code: 400,
      data: null,
    });
  } else {
    next();
  }
}
/**
 *@api {post} api/login  登录接口
 *@apiDescription  登录接口功能说明
 *@apiName login
 *@apiGroup login
 *@apiBody  {String} username 用户名
 *@apiBody  {String} password 密码
 *@apiBody  {String} captcha 验证码
 *@apiSuccessExample {json} Success-Response:
 *{
 *	 "msg": "登录成功",
    "data": {
        "token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc0xvZ2luIjp0cnVlLCJ0aW1lIjoxNzQwNjQ2OTA4MjYxLCJpYXQiOjE3NDA2NDY5MDgsImV4cCI6MTc0MDY1NDEwOH0.GAt8COimo4zfw0kBc9wDw_3Sh5S4yZxoE-Gh4kp_LxY"
    }
 *}
 *@apiSampleRequest api/login
 *@apiVersion 1.0.0
 */

router.post("/login", [login_middleware, login_params], loginController.login);

module.exports = router;
